package com.finchina.myview.LayoutAnimations;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.Keyframe;
import android.animation.LayoutTransition;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.GridView;

import com.finchina.myview.R;

/**
 * 作者：黄赛赛 on 2015/10/10 16:56
 *
 * @version V1.0
 * @Description: ${LayoutAnimationController的使用}()
 */
public class LayoutAnimationActivity extends AppCompatActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layoutanimation);

        GridView gv_modules = (GridView) findViewById(R.id.gv_modules);
        MainModuleAnimationAdapter adapter = new MainModuleAnimationAdapter(this);
        gv_modules.setAdapter(adapter);


//        Animation animation = (Animation) AnimationUtils.loadAnimation(LayoutAnimationActivity.this, R.anim.list_anim);
//        LayoutAnimationController lac = new LayoutAnimationController(animation);
//        lac.setOrder(LayoutAnimationController.ORDER_REVERSE);
//        lac.setDelay(0.5f);//注意这个地方是以秒为单位，是浮点型数据，所以要加f
//        gv_modules.setLayoutAnimation(lac);




//        LayoutTransition mTransition = new LayoutTransition();
//        mTransition.setAnimator(
//                LayoutTransition.APPEARING,
//                 mTransition
//                        .getAnimator(LayoutTransition.APPEARING));
//        mTransition
//                .setAnimator(
//                        LayoutTransition.CHANGE_APPEARING,
//                        mTransition
//                                .getAnimator(LayoutTransition.CHANGE_APPEARING)
//                );
//        mTransition.setAnimator(
//                LayoutTransition.DISAPPEARING,
//                mTransition
//                        .getAnimator(LayoutTransition.DISAPPEARING));
//        mTransition.setAnimator(
//                LayoutTransition.CHANGE_DISAPPEARING,
//                 mTransition
//                        .getAnimator(LayoutTransition.CHANGE_DISAPPEARING)
//                        );
        setupCustomAnimations();
        gv_modules.setLayoutTransition(mTransitioner);

    }
    LayoutTransition  mTransitioner  = new LayoutTransition();
    // 生成自定义动画
    private void setupCustomAnimations() {
        // 动画：CHANGE_APPEARING
        // Changing while Adding
        PropertyValuesHolder pvhLeft = PropertyValuesHolder.ofInt("left", 0, 1);
        PropertyValuesHolder pvhTop = PropertyValuesHolder.ofInt("top", 0, 1);
        PropertyValuesHolder pvhRight = PropertyValuesHolder.ofInt("right", 0,
                1);
        PropertyValuesHolder pvhBottom = PropertyValuesHolder.ofInt("bottom",
                0, 1);
        PropertyValuesHolder pvhScaleX = PropertyValuesHolder.ofFloat("scaleX",
                1f, 0f, 1f);
        PropertyValuesHolder pvhScaleY = PropertyValuesHolder.ofFloat("scaleY",

                1f, 0f, 1f);
        final ObjectAnimator changeIn = ObjectAnimator.ofPropertyValuesHolder(
                this, pvhLeft, pvhTop, pvhRight, pvhBottom, pvhScaleX,
                pvhScaleY).setDuration(
                mTransitioner.getDuration(LayoutTransition.CHANGE_APPEARING));
        mTransitioner.setAnimator(LayoutTransition.CHANGE_APPEARING, changeIn);
        changeIn.addListener(new AnimatorListenerAdapter() {
            public void onAnimationEnd(Animator anim) {
                View view = (View) ((ObjectAnimator) anim).getTarget();
                view.setScaleX(1f);
                view.setScaleY(1f);
            }
        });

        // 动画：CHANGE_DISAPPEARING
        // Changing while Removing
        Keyframe kf0 = Keyframe.ofFloat(0f, 0f);
        Keyframe kf1 = Keyframe.ofFloat(.9999f, 360f);
        Keyframe kf2 = Keyframe.ofFloat(1f, 0f);
        PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe(
                "rotation", kf0, kf1, kf2);
        final ObjectAnimator changeOut = ObjectAnimator
                .ofPropertyValuesHolder(this, pvhLeft, pvhTop, pvhRight,
                        pvhBottom, pvhRotation)
                .setDuration(
                        mTransitioner
                                .getDuration(LayoutTransition.CHANGE_DISAPPEARING));
        mTransitioner.setAnimator(LayoutTransition.CHANGE_DISAPPEARING,
                changeOut);
        changeOut.addListener(new AnimatorListenerAdapter() {
            public void onAnimationEnd(Animator anim) {
                View view = (View) ((ObjectAnimator) anim).getTarget();
                view.setRotation(0f);
            }
        });

        // 动画：APPEARING
        // Adding
        ObjectAnimator animIn = ObjectAnimator.ofFloat(null, "rotationY", 90f,
                0f).setDuration(
                mTransitioner.getDuration(LayoutTransition.APPEARING));
        mTransitioner.setAnimator(LayoutTransition.APPEARING, animIn);
        animIn.addListener(new AnimatorListenerAdapter() {
            public void onAnimationEnd(Animator anim) {
                View view = (View) ((ObjectAnimator) anim).getTarget();
                view.setRotationY(0f);
            }
        });

        // 动画：DISAPPEARING
        // Removing
        ObjectAnimator animOut = ObjectAnimator.ofFloat(null, "rotationX", 0f,
                90f).setDuration(
                mTransitioner.getDuration(LayoutTransition.DISAPPEARING));
        mTransitioner.setAnimator(LayoutTransition.DISAPPEARING, animOut);
        animOut.addListener(new AnimatorListenerAdapter() {
            public void onAnimationEnd(Animator anim) {
                View view = (View) ((ObjectAnimator) anim).getTarget();
                view.setRotationX(0f);
            }
        });

    }
}
